﻿<?php

class DoApp extends BaseApp
{
/*用户购物开始*/
    function buy()
    {
	    if(!isset($_GET['oid']))$_GET['oid']=0;
	    if(!isset($_POST['oid']))$_POST['oid']=0;		
        $orderid = $_GET['oid'];
		if(!$orderid)$orderid = $_POST['oid'];
		$buyidstr ='';
        $orderdata = array();		
        if($orderid)
		{
	           $db = & get_db();		
               $orderdata = $db->get_one("SELECT * FROM `".DB_PREFIX."shoporderuser` WHERE id = '{$orderid}' and uid=".$GLOBALS['SCup']->SCup_user_msg['user_id']." and status = 1 and ifpay =0 LIMIT 1");	
	           if($orderdata['id'] == '')
	           {
		       $notice = &get_message(array('msg'=>'错误~该订单已经支付或不存在!','url'=>'?','title'=>'Notice'));
		       $notice ->display();			
	           }	
	           $buyidstr=$orderdata['productmsg'];
			   setcookie("buyidstr",",$buyidstr");	    
		}				
        $id = $_GET['id'];
		if(!isset($_GET['do']))$_GET['do']='';
		$do = $_GET['do'];		
		$tci_data = explode('_', $id);
		if(!isset($tci_data['0']))$tci_data['0']=0;
		if(!isset($tci_data['1']))$tci_data['1']=0;
		if(!isset($tci_data['2']))$tci_data['2']=0;
		if(!isset($tci_data['3']))$tci_data['3']=1;						
		$tableid = intval($tci_data['0']);
		$catid = intval($tci_data['1']);		
		$productid = intval($tci_data['2']);
		$num = intval($tci_data['3']);		
	    if($productid<=0||$tableid<=0)
		{
		 $notice = &get_message(array('msg'=>'产品编号错误!','url'=>'','title'=>'Notice'));
		 $notice ->display();				
		}
		if(!$orderid)
		{
        $buyidstr=$_COOKIE['buyidstr'];			
		}
		if($do == 'cancel')/*取消该订单*/
		{
		     $db = & get_db();	  
  	         $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET `status`='0' WHERE id='$orderid' and uid=".$GLOBALS['SCup']->SCup_user_msg['user_id']);	
		     $notice = &get_message(array('msg'=>'该订单已经取消!','url'=>"?app=user&act=productorder",'title'=>'Notice'));
		     $notice ->display();		
		}		
		elseif($do == 'buy')
		{
		   $tempid = "{$tableid}_{$catid}_{$productid}_{$num}";		
	       if( strstr($buyidstr,",{$tableid}_{$catid}_{$productid}_") )
		   {
		    $notice = &get_message(array('msg'=>'该产品已经在购物车里了！','url'=>"?app=do&act=buy&id=$tempid",'title'=>'Notice'));
		    $notice ->display();	
	       } 
	       if( !strstr($buyidstr,",{$tableid}_{$catid}_{$productid}_") )
		   {
		    $buyidstr = "$buyidstr,$tempid,";
		    setcookie("buyidstr",$buyidstr);
	       }
		  if($orderid)
		  {	
		     $db = & get_db();	  
  	         $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET `productmsg`='$buyidstr' WHERE id='$orderid' and uid=".$GLOBALS['SCup']->SCup_user_msg['user_id']);		  		  
		  }  
	       echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=?app=do&act=buy&id=$tempid&oid=$orderid'>";
	       exit;		
		}
        elseif($do=="del")
        {
	      $detail=del_empty_var(explode(",",$buyidstr));
		  $tempid = "{$tableid}_{$catid}_{$productid}_{$num}";
	      foreach( $detail AS $key=>$value)
		  {
		     if($value==$tempid)
		     {
			  unset($detail[$key]);
		     }
	      }
	      $buyidstr=implode(",",$detail);
		  $buyidstr = ",$buyidstr,";
	      setcookie("buyidstr",buyidstr);
		  if($orderid)
		  {	
		     $db = & get_db();	  
  	         $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET `productmsg`='$buyidstr' WHERE id='$orderid' and uid=".$GLOBALS['SCup']->SCup_user_msg['user_id']);		  		  
		  }
	      echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=?app=do&act=buy&id=$tempid&oid=$orderid'>";
	      exit;
        }
        elseif($do=='updateNum')
        {
		   $tempid = "{$tableid}_{$catid}_{$productid}_{$num}";		
	       $buyidstr='';
		   $product = $_POST['product'];
	       foreach( (array)$product as $key=>$value)
		   {
		      if($value>500)
			  {
		      $notice = &get_message(array('msg'=>'单个商品,购买数量不能大于500个','url'=>'','title'=>'Notice'));
		      $notice ->display();	 
		      }
		      $temp_data = explode('_', $key);
		      if(!isset($temp_data['0']))$temp_data['0']=0;
		      if(!isset($temp_data['1']))$temp_data['1']=0;
		      if(!isset($temp_data['2']))$temp_data['2']=0;
		      if(!isset($temp_data['3']))$temp_data['3']=1;			  
			  $buyidstr.=$temp_data['0'].'_'.$temp_data['1'].'_'.$temp_data['2'].'_'.$value.",";
	       }
		   $buyidstr = ",$buyidstr";
	       setcookie("buyidstr",$buyidstr);
		  if($orderid)
		  {	
		     $db = & get_db();	  
  	         $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET `productmsg`='$buyidstr' WHERE id='$orderid' and uid=".$GLOBALS['SCup']->SCup_user_msg['user_id']);		  		  
		  }		   
	       echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=?app=do&act=buy&id=$tempid&oid=$orderid'>";
	       exit;
        }		
        elseif($do=='order')
       {
		  $uid = $GLOBALS['SCup']->SCup_user_msg['user_id'];
		  $username = $GLOBALS['SCup']->SCup_user_msg['data']['user_name'];			  		   
	      $buyer=$_POST['buyer'];
	      $sex=$_POST['sex'];
	      $telphone=$_POST['telphone'];
	      $mobphone=$_POST['mobphone'];
	      $email=$_POST['email'];
	      $oicq=$_POST['oicq'];
	      $postalcode=$_POST['postalcode'];
	      $sendtype=$_POST['sendtype'];
	      $paytype=$_POST['paytype'];
	      $address=$_POST['address'];
	      $otherSay=$_POST['otherSay'];	 
	      if(!$buyidstr)
	      {
		   $notice = &get_message(array('msg'=>'请不要重复提交','url'=>'','title'=>'Notice'));
		   $notice ->display();	 
	      }
	      if(!ereg("[0-9]+",$buyidstr))
		  {
		   $notice = &get_message(array('msg'=>'你并没有购买任何一件商品','url'=>'','title'=>'Notice'));
		   $notice ->display();			  
	      }
	      if(!$buyer)
	      {
		   $notice = &get_message(array('msg'=>'顾客姓名不能为空','url'=>'','title'=>'Notice'));
		   $notice ->display();			  
	      }
	      elseif(!$mobphone)
	      {
		   $notice = &get_message(array('msg'=>'联系手机号码不能为空','url'=>'','title'=>'Notice'));
		   $notice ->display();			  
	      }
	      if(!ereg("^1[0-9]{10}$",$mobphone))
		  {
		   $notice = &get_message(array('msg'=>'手机号码有误','url'=>'','title'=>'Notice'));
		   $notice ->display();			  
	      }
	      if($paytype=='')
	      {
		  $notice = &get_message(array('msg'=>'您没有选择支付方式!','url'=>'?','title'=>'Notice'));
		  $notice ->display();			   
	      }			  
	      $totalmoney=0;
	      $shopmoney=0;/*统计积分数*/
	      $db = & get_db();	
          $getdata = $this->buyidstr_to_product_msg($buyidstr,1);
          if(!$getdata['check'])
		  {
		    $notice = &get_message(array('msg'=>'你购买的产品当中，有一个产品已经不存在了，请先删除该产品在下单。','url'=>'?','title'=>'Notice'));
		    $notice ->display();		  
		  }	
          if(!$getdata['numcheck'])
		  {
		    $notice = &get_message(array('msg'=>'你购买的产品当中，有一个产品的库存不足，请先删除该产品在下单。','url'=>'?','title'=>'Notice'));
		    $notice ->display();		  
		  }			  	
	   	  $buyarray = $getdata['buy'];
		  $productdata = $getdata['product'];
		  $shopmoney = $getdata['shopmoney'];		  
		  $totalmoney = $getdata['totalmoney'];
		  $totalmoney_1= $totalmoney;
		  if($sendtype) {
		      $TB_peisong = &$this->_model('peisong');           		  
		      $peisong = $TB_peisong->getmoney($sendtype);	
			  $totalmoney+=$peisong;
		  }
		  $time = time();		  
		  if(!$orderid)
		  { 
		  	$TB_buy = &$this->_model('buy');
            $orderid=$TB_buy->getorderid();
			$db->query("INSERT INTO `".DB_PREFIX."shoporderuser` (`id` , `uid` ,`username` , `truename` , `sex` , `telphone` , `mobphone` , `email` , `oicq` , `postalcode` , `sendtype` , `paytype` ,  `olpaytype` , `address` , `othersay` , `posttime` , `totalmoney` , `productmsg`) 
				VALUES 
			('$orderid','$uid','$username','$buyer','$sex','$telphone','$mobphone','$email','$oicq','$postalcode','$sendtype','$paytype','$olpaytype','$address','$otherSay','$time','$totalmoney','$buyidstr')");
/*			@extract($db->get_one("SELECT id AS orderid FROM `".DB_PREFIX."shoporderuser` ORDER BY id DESC LIMIT 1"));*/
		  }else
		  {
		    $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET `truename`='$buyer',`sex`='$sex',`telphone`='$telphone',`mobphone`='$mobphone',`email`='$email',`oicq`='$oicq',`postalcode`='$postalcode',`sendtype`='$sendtype',`paytype`='$paytype',`olpaytype`='$olpaytype',`address`='$address',`othersay`='$otherSay',`posttime`='$time',`totalmoney`='$totalmoney',`productmsg`='$buyidstr' WHERE id='$orderid' and uid=".$GLOBALS['SCup']->SCup_user_msg['user_id']);
		  }
	      setcookie("buyidstr",'');				
          if($paytype=='-1'&&$totalmoney>0)/*预存款支付*/
		  {
		  	 $usermoney = $db->get_one("SELECT money FROM `".DB_PREFIX."users` WHERE user_id = '{$uid}' LIMIT 1");
			 $usermoney = $usermoney['money']; 	
	         if($usermoney>=$totalmoney)
			 {
			    $TB_user = &$this->_model('user');
	            $pay = $TB_user->moneypay($totalmoney,$GLOBALS['SCup']->SCup_user_msg['user_id'],$shopmoney);
		        foreach($productdata as $key => $value)
		        {
                    foreach($value as $key1 => $value1)
			        {
				       $key_str = $key.'_'.$value1['cat_id'].'_'.$value1['id'];
				       if($value1['shopnum']>0)
				       {
				       $db->query("UPDATE `".DB_PREFIX."content_".$key."` SET `shopnum`=`shopnum`-".$buyarray[$key_str]['3']." WHERE id='".$value1['id']."'");				
				       }							    
			        } 		
		       }
 	           $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET ifpay=1,paytime={$time} WHERE id='$orderid'");			    				
               $ary=array(
				   'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
				   'type' => '0',
				   'name' => '使用预存款支付',
				   'intro' => '购买商品使用预存款支付,支付总金额'.$totalmoney.'元（商品总价格：'.$totalmoney_1.'，运输方式：'.$sendtype.'，运输费用：'.($totalmoney-$totalmoney_1).'），订单号是'.$orderid,
				   'money' => $totalmoney,
				   'time' => time(),
		        );
		        $TB_user->moneylog($ary);
				$ary=array(
				'address'=> $GLOBALS['SCup']->SCup_user_msg['data']['user_email'],
				'username'=>$GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
				'title'=>'购物成功提示信息',
				'content'=>'',
				'addtime'=>time()
				);
		        $TB_user->mailCron($ary);
				$this->doqueue($GLOBALS['SCup']->SCup_user_msg['user_id'],$GLOBALS['SCup']->SCup_user_msg['data']['user_name'],$getdata,$order['productmsg'],$totalmoney_1,$totalmoney-$totalmoney_1,$orderid);				
		        $notice = &get_message(array('msg'=>'恭喜你,使用预存款支付成功,请牢记你的订单号,方便查询,你的订单号是:'.$orderid,'url'=>'?','title'=>'Notice'));
		        $notice ->display();	
	         }else
			 {
		      $notice = &get_message(array('msg'=>'你账上余额为'.$usermoney.'元，余额不足，请先充值或选择其它支付方式.','url'=>'?','title'=>'Notice'));
		      $notice ->display();	
	         }		
		
		  }


	      $TB_payment = &$this->_model('payment');
	      $pay = $TB_payment->paychoose(intval($paytype));
		  
	      $pay['orderid'] = $orderid;
	      $pay['price']=$totalmoney;
	      $pay['name']='购买产品';		
	      if($pay['is_cod']!=1)
	      {
			$returnurl = SITE_URL.'/?app=do&act=dorepay';
			$bottom=$this->dotopay($pay['pay_code'],$returnurl,$pay);
            $this->assign('bottom', $bottom);			
	      }		
	      $pay['pay_desc']=nl2br($pay['pay_desc']);	
          $this->assign('pay', $pay);
          $this->assign('order',  $order);
          $this->assign('price',  $price);		
          $this->assign('orderid',  $orderid);		
          $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	
		  if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		  {
          $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		  }	    
          $this->assign('view_charset', VIEW_CHARSET);	
          $this->assign('page_title', SITE_TITLE);
          $this->assign('page_description', SITE_DESCRIPTION);
          $this->assign('page_keywords', SITE_KEYWORDS);		
		  $this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
		        
		  $this->assign('site_url',SITE_URL);
          $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));		
          $this->display('group/buy_pay.view.html');
		  exit;
        }
		$TB_peisong = &$this->_model('peisong'); 		
		$peisong = $TB_peisong->show();	  
	
		
		$moduledata	= array();	
	    $tempmoduledata =  & $GLOBALS['SCup']->_get_module_list_data();
		foreach($tempmoduledata as $key => $value)
		{
            $moduledata[$value['id']] = $key;
		}
        unset($tempmoduledata);		
        $getdata = $this->buyidstr_to_product_msg($buyidstr);	
		$buyarray = $getdata['buy'];
		$productdata = $getdata['product'];
	    $TB_payment = &$this->_model('payment');
	    $pay = $TB_payment->paytype();
	    $this->assign('pay',$pay);
        $this->assign('productdata', $productdata);
        $this->assign('peisong', $peisong);		
        $this->assign('buyarray', $buyarray);
        $this->assign('moduledata',  $moduledata);
        $this->assign('order',  $orderdata);		
        $this->assign('oid',  $orderid);				  
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		       
        $this->assign('site_url', SITE_URL);
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));		
        $this->display('do_buy.view.html');
    }

    function buyidstr_to_product_msg($buyidstr,$ischeck=0)
	{
		$return_data = array();
		$productdata = array();
		$productcheck = array();
		$shopmoney = 0 ;
		$totalmoney = 0 ;		
		$numcheck = true;		
		$check = true ;		
		$buyarray = $this->buyidstr_to_array($buyidstr);				
        if(!empty($buyarray))
		{
		   $sql_in_str = array();
		   foreach($buyarray as $key=>$value)
		   {
		       if(!isset($sql_in_str[$value['0']]))$sql_in_str[$value['0']]='';
		       $sql_in_str[$value['0']].=$value['2'].',';
		   }
	       $db = & get_db();		   
		   foreach($sql_in_str as $key =>$value)
		   {
		      $fids= substr($value,0,-1);
			  $productdata[$key] = $db->get_all("SELECT * FROM ".DB_PREFIX."content_".$key." WHERE id IN ($fids)");			  
		   }   		   		  
		}
		if($ischeck&&!empty($buyarray)&&!empty($productdata))
		{
		    foreach($productdata as $key => $value)
			{
			    foreach($value as $key1 => $value1)
				{
				   $keystr = $key .'_'. $value1['cat_id'] .'_'. $value1['id'];
				   $productcheck[$keystr]['check']=1;
				   $productcheck[$keystr]['shoptype']=$value1['shoptype'];
				   $productcheck[$keystr]['numcheck']=$value1['shopnum'];
				   $shopmoney+=$value1['shopmoney'];
		           $totalmoney+=$value1['nowprice']*$buyarray[$keystr]['3'];
				}				
			}					
		    foreach($buyarray as $key => $value)
			{
				if(!isset($productcheck[$key]))
				{
				$productcheck[$key]['check']=0;
				$productcheck[$key]['name']='';
				$productcheck[$key]['numcheck']=0;					
				$check = false;					
				}else
				{
				   if($productcheck[$key]['numcheck']>=$value['3'])
				   {
				     $productcheck[$key]['numcheck']=$value['3'];				   
				   }else
				   {
				     $productcheck[$key]['numcheck']=0;	
					 $numcheck = false;				   
				   }			
				}				
			}
		}
		$return_data['buy'] = $buyarray;
		$return_data['product'] = $productdata;
		$return_data['productcheck'] = $productcheck;		
		$return_data['check'] = $check;	
		$return_data['numcheck'] = $numcheck;
		$return_data['shopmoney'] = $shopmoney;	
		$return_data['totalmoney'] = $totalmoney;							
		return $return_data;		
	}

    function buyidstr_to_array($buyidstr)
	{
        $detail=del_empty_var(explode(",",$buyidstr));
		$buyarray = array();
        foreach( $detail AS $key=>$value)
		{
		   $temp_data = explode('_', $value);
		   if(!isset($temp_data['0']))$temp_data['0']=0;
		   if(!isset($temp_data['1']))$temp_data['1']=0;
		   if(!isset($temp_data['2']))$temp_data['2']=0;
		   if(!isset($temp_data['3']))$temp_data['3']=1;		   						   
	       if(intval($temp_data['2'])>0||intval($temp_data['0'])>0)
		   {
		      $key_str = $temp_data['0'].'_'.$temp_data['1'].'_'.$temp_data['2'];
              $buyarray[$key_str]['0']=$temp_data[0];
              $buyarray[$key_str]['1']=$temp_data[1];			  
              $buyarray[$key_str]['2']=$temp_data[2];
              $buyarray[$key_str]['3']=$temp_data[3];			  			  
	       }
        }
		return $buyarray;		
	}


 function actpay($price,$orderid)
 {

 	     if($price=='' || $orderid=='')
	     {
		 $notice = &get_message(array('msg'=>'首页验证支付失败!','url'=>'?','title'=>'Notice'));
		 $notice ->display();
	     }
	     if($price>0)
	     {
		    $price = float($price);
			$TB_user = &$this->_model('user');
	        $pay = $TB_user->moneyadd($price,$GLOBALS['SCup']->SCup_user_msg['user_id']);
            $ary=array(
				'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
				'type' => 1,
				'name' => '充值账户',
				'intro' => '您为账户充值'.$price.'元',
				'money' => $price,
				'time' => time(),
		   );
		   $TB_user->moneylog($ary);
	      }
	
	      $db = & get_db();		
          $order = $db->get_one("SELECT * FROM `".DB_PREFIX."shoporderuser` WHERE id = '{$orderid}' and status = 1 and ifpay =0 LIMIT 1");	
	      if($order['id'] == '')
	      {
		     $notice = &get_message(array('msg'=>'错误~该订单已经支付或不存在!','url'=>'?','title'=>'Notice'));
		     $notice ->display();			
	      }
	      $totalmoney=0;
	      $shopmoney=0;/*统计积分数*/
          $getdata = $this->buyidstr_to_product_msg($order['productmsg'],1);
          if(!$getdata['check'])
		  {
		    $notice = &get_message(array('msg'=>'你购买的产品当中，有一个产品已经不存在了，请先删除该产品在下单。','url'=>'?','title'=>'Notice'));
		    $notice ->display();		  
		  }	
          if(!$getdata['numcheck'])
		  {
		    $notice = &get_message(array('msg'=>'你购买的产品当中，有一个产品的库存不足，请先删除该产品在下单。','url'=>'?','title'=>'Notice'));
		    $notice ->display();		  
		  }			  	
	   	  $buyarray = $getdata['buy'];
		  $productdata = $getdata['product'];
		  $shopmoney = $getdata['shopmoney'];		  
		  $totalmoney = $getdata['totalmoney'];
		  $totalmoney_1= $totalmoney;		
		  if($sendtype) {
		      $TB_peisong = &$this->_model('peisong');           		  
		      $peisong = $TB_peisong->getmoney($sendtype);	
			  $totalmoney+=$peisong;
		  }
		  $time = time();
          $uid = $GLOBALS['SCup']->SCup_user_msg['user_id'];
		  $usermoney = $db->get_one("SELECT money FROM `".DB_PREFIX."users` WHERE user_id = '{$uid}' LIMIT 1");
		  $usermoney = $usermoney['money']; 	
	      if($usermoney>=$totalmoney)
		  {
			    $TB_user = &$this->_model('user');
	            $pay = $TB_user->moneypay($totalmoney,$GLOBALS['SCup']->SCup_user_msg['user_id'],$shopmoney);
		        foreach($productdata as $key => $value)
		        {
                    foreach($value as $key1 => $value1)
			        {
				       $key_str = $key.'_'.$value1['cat_id'].'_'.$value1['id'];
				       if($value1['shopnum']>0)
				       {
				       $db->query("UPDATE `".DB_PREFIX."content_".$key."` SET `shopnum`=`shopnum`-".$buyarray[$key_str]['3']." WHERE id='".$value1['id']."'");				
				       }							    
			        } 		
		       }
 	           $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET ifpay=1,paytime={$time} WHERE id='$orderid'");			    				
               $ary=array(
				   'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
				   'type' => 0,
				   'name' => '使用预存款支付',
				   'intro' => '购买商品使用预存款支付,支付总金额'.$totalmoney.'元（商品总价格：'.$totalmoney_1.'，运输方式：'.$order['sendtype'].'，运输费用：'.($totalmoney-$totalmoney_1).'），订单号是'.$orderid,
				   'money' => $totalmoney,
				   'time' => time(),
		        );
		        $TB_user->moneylog($ary);
				$ary=array(
				'address'=> $GLOBALS['SCup']->SCup_user_msg['data']['user_email'],
				'username'=>$GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
				'title'=>'购买成功提示信息',
				'content'=>'',
				'addtime'=>time()
				);
		        $TB_user->mailCron($ary);
				$this->doqueue($GLOBALS['SCup']->SCup_user_msg['user_id'],$GLOBALS['SCup']->SCup_user_msg['data']['user_name'],$getdata,$order['productmsg'],$totalmoney_1,$totalmoney-$totalmoney_1,$orderid);
				
		        $notice = &get_message(array('msg'=>'恭喜你,使用预存款支付成功,请牢记你的订单号,方便查询,你的订单号是:$orderid','url'=>'?','title'=>'Notice'));
		        $notice ->display();	
	     }else
		 {
		      $notice = &get_message(array('msg'=>'你账上余额为'.$usermoney.'元，余额不足，请先充值或选择其它支付方式.','url'=>'?','title'=>'Notice'));
		      $notice ->display();	
	     }	

 }
	function doqueue($userid=0,$username='',$getdata=array(),$productmsg = '',$productmoney=0,$sendmoney=0,$orderid=0)
	{
		$productdata = $getdata['product'];
	    $db = & get_db();
		$t_queuetypedata = $db->get_all("SELECT id,table_id,cat_id,productid,queuemoney FROM `".DB_PREFIX."queuetype` WHERE table_id>0 ");	
		$queuetypedata = array();
		$queuetypeid_data = array();
		foreach($t_queuetypedata as $key => $value)
		{
		     $queuetypedata[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['queuemoney'];
			 $queuetypeid_data[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['id'];
		}
        $tongji = array();
		$queuemoneydata = array();
		foreach($productdata as $key => $value)
		{
             foreach($value as $key1 => $value1)
			 {
			      $queuemoney = 100 ;
				  $icon = 'default';
				  $queueid = 0  ;
				  if(isset($queuetypedata['0_0_0']))
				  {
					 $queuemoney = $queuetypedata['0_0_0']; 
					 $queueid = $queuetypeid_data['0_0_0'];
					 $icon = 'all'; 
				  }				  
				  if(isset($queuetypedata[$key.'_0_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_0_0']; 
					 $queueid = $queuetypeid_data[$key.'_0_0'];
					 $icon = 'table'.$key;  
				  }
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_0']; 
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_0'];
					 $icon = 'cat'.$key.'_'.$value1['cat_id'];
				  }				  				    
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']]))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $icon = 'product'.$key.'_'.$value1['cat_id'].'_'.$value1['id'];
				  }
				  $queuemoneydata[$icon] = $queuemoney;
				  $queueiddata[$icon] = $queueid;
				  $tongji[$icon][$key1]['data'] = $value1;
				  $tongji[$icon][$key1]['buynum'] = $getdata['buy'][$key.'_'.$value1['cat_id'].'_'.$value1['id']][3];
				  $tongji[$icon][$key1]['money'] = $value1['nowprice'];
				  $tongji[$icon][$key1]['tableid'] = $key;
				  $tongji[$icon][$key1]['cat_id'] = $value1['cat_id'];
				  $tongji[$icon][$key1]['id'] = $value1['id'];
				  	    
			 } 		
		}
       $sql = 'insert into '.DB_PREFIX.'queuemsg(uid,username,table_id,cat_id,productid,queuemoney,add_time,paytime,productmsg,totalmoney,onemoney,buynum,orderid,queueid) VALUES';
        $time = time();	
		$start = 0 ;	
		foreach($tongji as $key => $value)
		{
		    $kkmoneytotal =0;
			$tb = 0 ;
			$ct = 0 ;
			$pi = 0 ;
			$buynum = 0 ;
			$queuemoney = $queuemoneydata[$key];
			$queueid = $queueiddata[$key];
		    foreach($value as $key1 => $value1)
			{
			  $kkmoneytotal += $value1['money'] * $value1['buynum'] ;
			  $buynum += $value1['buynum'] ;
			  $tb = $value1['tableid'] ;
			  $ct = $value1['cat_id'] ;
			  $pi = $value1['id'] ;			  
			}
		    $kk = intval($kkmoneytotal / $queuemoney);

			if($kk >=1)
			{
			    for($i= 0 ; $i < $kk ;$i++)
			    {
				   $time = $time + $i ;
			      $sql .= "('{$userid}','{$username}','{$tb}','{$ct}','{$pi}','{$queuemoney}','{$time}','{$time}','{$productmsg}','{$productmoney}','{$kkmoneytotal}','{$buynum}','{$orderid}','{$queueid}'),";   
			    }			
			    $start = 1;
			}		 
		}
		if($start)
		{
		    $sql = substr($sql, 0,-1) ;
		    if($db->query($sql))
		    {
		     return 1 ;
		    }
			return 0 ;			
		}
		return 'no';
	}
 
 	function doqueue_1($userid=0,$username='',$getdata=array(),$productmsg = '',$productmoney=0,$sendmoney=0,$orderid=0)
	{
		onethread();
		$queue_num = QUEUENUM ;	
		$productdata = $getdata['product'];
	    $db = & get_db();
		$t_queuetypedata = $db->get_all("SELECT id,table_id,cat_id,productid,queuemoney FROM `".DB_PREFIX."queuetype` WHERE table_id>0 ");	
		$queuetypedata = array();
		$queuetypeid_data = array();
		foreach($t_queuetypedata as $key => $value)
		{
		     $queuetypedata[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['queuemoney'];
			 $queuetypeid_data[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['id'];
		}
        $tongji = array();
		$queuemoneydata = array();
		foreach($productdata as $key => $value)
		{
             foreach($value as $key1 => $value1)
			 {
			      $queuemoney = 100 ;
				  $icon = 'default';
				  $queueid = 0  ;
				  if(isset($queuetypedata['0_0_0']))
				  {
					 $queuemoney = $queuetypedata['0_0_0']; 
					 $queueid = $queuetypeid_data['0_0_0'];
					 $icon = 'all'; 
				  }				  
				  if(isset($queuetypedata[$key.'_0_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_0_0']; 
					 $queueid = $queuetypeid_data[$key.'_0_0'];
					 $icon = 'table'.$key;  
				  }
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_0']; 
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_0'];
					 $icon = 'cat'.$key.'_'.$value1['cat_id'];
				  }				  				    
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']]))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $icon = 'product'.$key.'_'.$value1['cat_id'].'_'.$value1['id'];
				  }
				  $queuemoneydata[$icon] = $queuemoney;
				  $queueiddata[$icon] = $queueid;
				  $tongji[$icon][$key1]['data'] = $value1;
				  $tongji[$icon][$key1]['buynum'] = $getdata['buy'][$key.'_'.$value1['cat_id'].'_'.$value1['id']][3];
				  $tongji[$icon][$key1]['money'] = $value1['nowprice'];
				  $tongji[$icon][$key1]['tableid'] = $key;
				  $tongji[$icon][$key1]['cat_id'] = $value1['cat_id'];
				  $tongji[$icon][$key1]['id'] = $value1['id'];
				  	    
			 } 		
		}
       $sql = 'insert into '.DB_PREFIX.'queuemsg_1(uid,username,table_id,cat_id,productid,queuemoney,add_time,paytime,productmsg,totalmoney,onemoney,buynum,orderid,queueid,isqueue,isyushu,isyouhui) VALUES';
        $time = time();	
		$start = 0 ;	
		foreach($tongji as $key => $value)
		{
		    $kkmoneytotal =0;
			$tb = 0 ;
			$ct = 0 ;
			$pi = 0 ;
			$buynum = 0 ;
			$queuemoney = $queuemoneydata[$key];
			$queueid = $queueiddata[$key];
		    foreach($value as $key1 => $value1)
			{
			  $kkmoneytotal += $value1['money'] * $value1['buynum'] ;
			  $buynum += $value1['buynum'] ;
			  $tb = $value1['tableid'] ;
			  $ct = $value1['cat_id'] ;
			  $pi = $value1['id'] ;			  
			}
		    $kk = intval($kkmoneytotal / $queuemoney);			
            $youhui_num = intval($kk / $queue_num );
			$yushu_num = $kk % $queue_num;
			$isqueue = $isyushu = $isyouhui = 0 ;
			$youhui_array = $yushu_array = array();
			if($youhui_num)
			{
				$youhui_array[1] = 1 ;
				if($youhui_num>1)
				{  $k = 1 ;
			       for($i= 1 ; $i < $youhui_num ;$i++)
			       {
				     $k = $k + $queue_num ;
					$youhui_array[$k] = $k ;
			       }						
				}
			}
			if($yushu_num)
			{
			    for($i= 0 ; $i < $yushu_num ;$i++)
			    {
					$yushu_array[$kk - $i] = $kk - $i ;
			    }						
			}
		
			if($kk >=1)
			{
			    for($i= 1 ; $i <= $kk ;$i++)
			    {
				   $isqueue = $isyushu = $isyouhui = 0 ;
				   $time = $time + $i ;
				   if($youhui_num && isset($youhui_array[$i]) && !empty($youhui_array[$i]) )
				   {
					  $isqueue = 1; 
					  $isyouhui = 1;
				   }
				   if($yushu_num && isset($yushu_array[$i]) && !empty($yushu_array[$i]) )
				   {
					  $isyushu = 1; 
				   }				   
			       $sql .= "('{$userid}','{$username}','{$tb}','{$ct}','{$pi}','{$queuemoney}','{$time}','{$time}','{$productmsg}','{$productmoney}','{$kkmoneytotal}','{$buynum}','{$orderid}','{$queueid}','{$isqueue}','{$isyushu}','{$isyouhui}'),";  
				    
			    }			
			    $start = 1;
			}		 
		}
		if($start)
		{
		    $sql = substr($sql, 0,-1) ;
		    if($db->query($sql))
		    {
			  $start1 = 0;	
		      foreach($tongji as $key => $value)
		      {	
			   $queueid = $queueiddata[$key];			  			
		       $yushu_cha_data = $db->get_all("SELECT id FROM `".DB_PREFIX."queuemsg_1` WHERE isyushu=1 && isqueue=0 && queueid={$queueid}  ORDER BY add_time asc ");	
			   $isdayutotal = $db->get_one("SELECT COUNT(*) AS num  FROM `".DB_PREFIX."queuemsg_1` WHERE isqueue=0  && queueid={$queueid} ");
		       $isdayutotal=$isdayutotal['num'];
               $total_num_isyushu = count($yushu_cha_data);
			   $num = intval($total_num_isyushu / ($queue_num-1)) ;
			   $quxiaoyushu = array();
			   $queue_num_1 = $queue_num;
			   if($isdayutotal>=$queue_num && $num)
			   {
				   if($isdayutotal==$queue_num)
				   {
					  $queue_num_1 = $queue_num + 1; 
				   }
				   $i = 0 ;
				   $sql_str = $sql_str1 = ''; 
				   foreach($yushu_cha_data as $key => $value)
				   {
					  $i++ ;
					  if($i<= ($num*($queue_num_1-1) ))
					  {
					    $quxiaoyushu[$value['id']] = $value['id'];
						 $sql_str.="id='".$value['id']."'  or ";
					  }else
					  {
					   break ;
					  }  
				   }
				   $sql_str = substr($sql_str, 0,-3);
		           $queue_is_data = $db->get_all("SELECT id FROM `".DB_PREFIX."queuemsg_1` WHERE isqueue=0  && queueid={$queueid} ORDER BY add_time asc  LIMIT 0,{$num} ");	
				   foreach($queue_is_data as $key => $value)
				   {
					  $sql_str1.="id='".$value['id']."'  or ";
				   }	
				   $sql_str1 = substr($sql_str1, 0,-3);	
		           $db->query("UPDATE ".DB_PREFIX."queuemsg_1 SET isyushu=0 WHERE ".$sql_str." ");
		           $db->query("UPDATE ".DB_PREFIX."queuemsg_1 SET isqueue=1 WHERE ".$sql_str1." ");
				   $start1 = 1 ;			
			   }
			  }
			  if($start1)return 1 ;
		    }
			return 0 ;			
		}
		return 'no';
	}
 
   

 
function dorepay(){
	$pay_code = !empty($_REQUEST['pay']) ? trim($_REQUEST['pay']) : '';
    if($pay_code=='')
	{
		$notice = &get_message(array('msg'=>'错误！','url'=>'?','title'=>'Notice'));
		$notice ->display();
	}	
	include_once(ROOT_PATH . '/lib/pay/'.$pay_code.'.pay.php');
    $app_pay_class_name = ucfirst($pay_code);
    $app_pay     = new $app_pay_class_name();
	$msg=$app_pay->dopayRe();
	if(is_array($msg)){
		$this -> actpay($msg['price'],$msg['orderid']);
	}else{

		 $notice = &get_message(array('msg'=>'$msg','url'=>'?','title'=>'Notice'));
		 $notice ->display();	
	}
	exit;
}
/*用户购物结束*/

function dotopay($app,$returnurl,$pay)
{
	$payment=unserialize($pay['pay_config']);
	$returnurl.='&pay='.$app;
	include_once(ROOT_PATH . '/lib/pay/'.$app.'.pay.php');
    $app_pay_class_name = ucfirst($app);
    $app_pay     = new $app_pay_class_name();
	$bottom=$app_pay->payTo($payment,$returnurl,$pay);
	return $bottom;
	exit;
}


/*用户充值开始*/
    function addmoney()
    {

	    $TB_user = &$this->_model('user');
		$money = $TB_user->moneyuser();
	    $TB_payment = &$this->_model('payment');
	    $pay = $TB_payment->paytype();
        $this->assign('pay', $pay);
        $this->assign('money', $money);
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
			
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	        
		$this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
	        $this->assign('site_url',SITE_URL);	
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');	
		
        $this->display('user_addmoney.view.html');

    }	

    function doaddmoney()
    {
        if(!isset($_POST['money'])){ $_POST['money']='';}
        if(!isset($_POST['paytype'])){ $_POST['paytype']='';}		
        $money = ceil($_POST['money']);
        $paytype = intval($_POST['paytype']);
	    if($paytype=='')
	    {
		   $notice = &get_message(array('msg'=>'您没有选择支付方式或者没有适合的支付接口！','url'=>'','title'=>'Notice'));
		   $notice ->display();	
	    }

	    if(!is_numeric($money) || $money<=0)
	    {
			 $notice = &get_message(array('msg'=>'充值金额必须大于等于0！','url'=>'','title'=>'Notice'));
		     $notice ->display();	
	    }

	    $TB_payment = &$this->_model('payment');
	    $pay = $TB_payment->paychoose($paytype);

	    $pay['orderid']=time().rand(10000,99999);
	    $pay['price']=$money;
	    $pay['name']='账户充值';

	    $returnurl = SITE_URL.'/?app=do&act=readdmoney';
	    $bottom=$this -> dotopay($pay['pay_code'],$returnurl,$pay);

        $this->assign('bottom', $bottom);
        $this->assign('pay', $pay);
        $this->assign('money',  $money);
        $this->assign('paytype', $paytype);

		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
			
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	        
		$this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
	    $this->assign('site_url',SITE_URL);	
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');		
        $this->display('user_doaddmoney.view.html');

    }
    function readdmoney()
	{
	$pay_code = !empty($_REQUEST['pay']) ? trim($_REQUEST['pay']) : '';
    if($pay_code=='')
	{
			 $notice = &get_message(array('msg'=>'错误！','url'=>'?','title'=>'Notice'));
		     $notice ->display();
	}
	include_once(ROOT_PATH . '/lib/pay/'.$pay_code.'.pay.php');
    $app_pay_class_name = ucfirst($pay_code);
    $app_pay     = new $app_pay_class_name();
	$msg=$app_pay->addmymoney();
	if(is_array($msg)){
		$this -> doaddmoneypay($msg['price'],$msg['orderid']);
	}else{
		 $notice = &get_message(array('msg'=>'$msg','url'=>'?','title'=>'Notice'));
		 $notice ->display();	
	}
	exit;
    }

    function doaddmoneypay($price,$orderid)
   {
 	   if($price=='' || $orderid=='')
	   {
		 $notice = &get_message(array('msg'=>'支付失败!','url'=>'?','title'=>'Notice'));
		 $notice ->display();
	   }
	   if($price>0)
	   {
       	$TB_user = &$this->_model('user');
		$pay=$TB_user->moneyadd(intval($price),$GLOBALS['SCup']->SCup_user_msg['user_id']);
		$ary=array(
				'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
				'type' => 1,
				'name' => '充值账户',
				'intro' => '您为账户充值'.$price.'元',
				'money' => $price,
				'time' => time(),
		);
		$TB_user->moneylog($ary);
	   }
		$ary=array(
		'id' 	=> $orderid,
		'money' => $price,
		'userid'=> $GLOBALS['SCup']->SCup_user_msg['user_id'],
		'paytime'=> time(),
	     );
         $TB_user->addmoney($ary);
		 $notice = &get_message(array('msg'=>'充值成功！','url'=>'?app=user&act=money','title'=>'Notice'));
		 $notice ->display();

   }
/*用户充值结束*/


/*本地下载开始*/
/* 
?app=do&act=downfile&tbid=1&catid=1&id=1&filed=softurl 
tbid:表的id号
catid:分类id号
id:文章id号
filed:下载的路径字段名
*/
    function downfile()
   {
   
       $tbid = intval($_GET['tbid']);
       $catid = intval($_GET['catid']);
       $id = intval($_GET['id']);	   
       $filed = $_GET['filed'];		      
	    $db = & get_db();		
        $data= $db->get_one("SELECT * FROM `".DB_PREFIX."content_".$tbid."` WHERE id = '{$id}' and user_id=".$GLOBALS['SCup']->SCup_user_msg['user_id']." LIMIT 1");

       if($catid!=$data['cat_id'])
	   {
		 $notice = &get_message(array('msg'=>'catid有误','url'=>'','title'=>'Notice'));
		 $notice ->display();	   
	   }
       $time =time();	   
	   $temp_rand =$time.rand(10000,99999); 
       $file_dir = ROOT_PATH.'/'.UPFILE_PATH.'/'.$data[$filed];
      if(file_exists($file_dir))
      {
	    $this->update_hits($tbid,$id,$catid,$data['down_time']);	  
	  	$true_name = str_replace(strrchr($data[$filed],'.'),'',basename($data[$filed]));
	    $filetype = substr(strrchr($data[$filed],'.'),1);
	    $filename = $true_name.".".$filetype;
	    setcookie("phpscupdown_".$temp_rand,"$tbid\t$catid\t$id\t$filed\t".$filename,$time+10);				
	    header("location:".SITE_URL."/index.php?app=do&act=dodownfile&r=".$temp_rand."&f=".$data[$filed]);
	    exit;		

      }else
	  {
		 $notice = &get_message(array('msg'=>'该文件不存在了!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
      }
	   
   }
    function dodownfile()
   {
        $f = $_GET['f'];
        $r = $_GET['r'];
	    $istrue = true ;
	    $filed_istrue = true ;					
        list($tbid,$catid,$id,$filed,$fname)=explode("\t",$_COOKIE["phpscupdown_".$r]);

		if(empty($tbid)||empty($catid)||empty($id)||empty($filed)||empty($fname))
		{
		 $notice = &get_message(array('msg'=>'参数错误！','url'=>'','title'=>'Notice'));
		 $notice ->display();			
		}		
        $module_list_data = & $GLOBALS['SCup']->_get_module_list_data();	   	   
        foreach($module_list_data as $key => $value)
	    {
	      if($tbid == $value['id'])
 		  {
		      if($key.'/'.$id.'/'.$fname != $f)
			  {
		         $notice = &get_message(array('msg'=>'参数错误！11','url'=>'','title'=>'Notice'));
		         $notice ->display();				  
			  }
	          $istrue = false ; 
		      $config = unserialize($value['config']);
		      foreach($config as $key1 =>$value1)
		      {
		         if($value1['filed']['name']==$filed)
				 {
	              $filed_istrue = false ;
				 }
		      }  
		  }
	    }
       if($istrue)
	   {
		 $notice = &get_message(array('msg'=>'tbid有误','url'=>'','title'=>'Notice'));
		 $notice ->display();	   
	   }	   
       if($filed_istrue)
	   {
		 $notice = &get_message(array('msg'=>'filed有误','url'=>'','title'=>'Notice'));
		 $notice ->display();	   
	   }						
        $file_name = ROOT_PATH."/".UPFILE_PATH.'/'.$f;
	    $true_name = str_replace(strrchr($f,'.'),'',basename($f));
	    $filetype = substr(strrchr($f,'.'),1);
	    $filename = $true_name.".".$filetype;	 
       if (!file_exists($file_name)) 
	   {
		 $notice = &get_message(array('msg'=>'该文件不存在了!','url'=>'','title'=>'Notice'));
		 $notice ->display();
       } else 
	   {
       $file = fopen($file_name,"r");
       header("content-type: application/octet-stream"); 
       header("accept-ranges: bytes"); 
       header("accept-length: ".filesize($file_name)); 
       header("content-disposition: attachment; filename=" . "phpscup_down.".$filetype); 
       echo fread($file,filesize($file_name)); 
       fclose($file);	
       exit;
	   }    
   }
   //下载频更新点击率
   function update_hits($tbid,$id,$catid,$time=0)
   {
	    $timestamp = time();
		if(date("W",$time)!=date("W",$timestamp)){
			$SQL.=",week_down=1";
		}else{
			$SQL.=",week_down=week_down+1";
		}
		if(date("md",$time)!=date("md",$timestamp)){
			$SQL.=",day_down=1";
		}else{
			$SQL.=",day_down=day_down+1";
		}
		if(date("m",$time)!=date("m",$timestamp)){
			$SQL.=",month_down=1";
		}else{
			$SQL.=",month_down=month_down+1";
		}		
	    $db = & get_db();		
		$db->query("UPDATE ".DB_PREFIX."content_".$tbid." SET total_down=total_down+1,down_time='$timestamp'$SQL WHERE id='$id' AND cat_id='$catid'");
   }

   
/*本地下载结束*/

/*视频播放开始*/
/* 
?app=do&act=player&tbid=1&catid=1&id=1&filed=softurl 
tbid:表的id号
catid:分类id号
id:文章id号
filed:视频的路径字段名
*/
    function player()
   {
   
       $tbid = intval($_GET['tbid']);
       $catid = intval($_GET['catid']);
       $id = intval($_GET['id']);	   
       $filed = $_GET['filed'];		      
	    $db = & get_db();		
        $data= $db->get_one("SELECT * FROM `".DB_PREFIX."content_".$tbid."` WHERE id = '{$id}' and user_id=".$GLOBALS['SCup']->SCup_user_msg['user_id']." LIMIT 1");

       if($catid!=$data['cat_id'])
	   {
		 $notice = &get_message(array('msg'=>'catid有误','url'=>'','title'=>'Notice'));
		 $notice ->display();	   
	   }
       $time =time();	   
	   $temp_rand =$time.rand(10000,99999); 
       $file_dir = ROOT_PATH.'/'.UPFILE_PATH.'/'.$data[$filed];
      if(file_exists($file_dir))
      {
	   /*  $this->update_hits($tbid,$id,$catid,$data['down_time']);	 */  
	  	$true_name = str_replace(strrchr($data[$filed],'.'),'',basename($data[$filed]));
	    $filetype = substr(strrchr($data[$filed],'.'),1);
	    $filename = $true_name.".".$filetype;
	    setcookie("phpscupdown_".$temp_rand,"$tbid\t$catid\t$id\t$filed\t".$filename,$time+10);				
	    header("location:".SITE_URL."/index.php?app=do&act=doplayer&r=".$temp_rand."&f=".$data[$filed]);
	    exit;		

      }else
	  {
		 $notice = &get_message(array('msg'=>'该文件不存在了!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
      }
	   
   }
    function doplayer()
   {
        $f = $_GET['f'];
        $r = $_GET['r'];
	    $istrue = true ;
	    $filed_istrue = true ;					
        list($tbid,$catid,$id,$filed,$fname)=explode("\t",$_COOKIE["phpscupdown_".$r]);

		if(empty($tbid)||empty($catid)||empty($id)||empty($filed)||empty($fname))
		{
		 $notice = &get_message(array('msg'=>'参数错误！','url'=>'','title'=>'Notice'));
		 $notice ->display();			
		}		
        $module_list_data = & $GLOBALS['SCup']->_get_module_list_data();	   	   
        foreach($module_list_data as $key => $value)
	    {
	      if($tbid == $value['id'])
 		  {
		      if($key.'/'.$id.'/'.$fname != $f)
			  {
		         $notice = &get_message(array('msg'=>'参数错误！11','url'=>'','title'=>'Notice'));
		         $notice ->display();				  
			  }
	          $istrue = false ; 
		      $config = unserialize($value['config']);
		      foreach($config as $key1 =>$value1)
		      {
		         if($value1['filed']['name']==$filed)
				 {
	              $filed_istrue = false ;
				 }
		      }  
		  }
	    }
       if($istrue)
	   {
		 $notice = &get_message(array('msg'=>'tbid有误','url'=>'','title'=>'Notice'));
		 $notice ->display();	   
	   }	   
       if($filed_istrue)
	   {
		 $notice = &get_message(array('msg'=>'filed有误','url'=>'','title'=>'Notice'));
		 $notice ->display();	   
	   }						
        $file_name = ROOT_PATH."/".UPFILE_PATH.'/'.$f;
	    $true_name = str_replace(strrchr($f,'.'),'',basename($f));
	    $filetype = substr(strrchr($f,'.'),1);
	    $filename = $true_name.".".$filetype;	 
       if (!file_exists($file_name)) 
	   {
		 $notice = &get_message(array('msg'=>'该文件不存在了!','url'=>'','title'=>'Notice'));
		 $notice ->display();
       } else 
	   {

        $width=500;	//视频宽度,可以自由更改
        $height=400; //视频高度,可以自由更改

        $urlstring=urlencode(SITE_URL."/".UPFILE_PATH.'/'.$f);
        $_height=$height+70;
        $_width=$width-5;
        $extend = pathinfo($f);
        $type = strtolower($extend["extension"]);
		
echo "
<head>
<title>phpscup video</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<style type=\"text/css\">
*{
	font-size: 12px;
}
</style>
<SCRIPT LANGUAGE='JavaScript' src='".SITE_URL."/?app=do&act=playerurl&urlstring=$urlstring'></SCRIPT>
</head>
<body oncontextmenu=\"window.event.returnValue=false\" scroll=\"no\" ondragstart=\"window.event.returnValue=false\" onselectstart=\"event.returnValue=false\" topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<SCRIPT LANGUAGE=\"JavaScript\">
phpscup_player(playurl,'$width','$height','$type','autostart');
</SCRIPT>
</body>
</html>
<SCRIPT LANGUAGE=\"JavaScript\">
<!--
self.resizeTo($_width,$_height);
//-->
</SCRIPT>
";
   
            exit;
	   }    
    }
    function playerurl()
   {
            $urlstring=urldecode($_GET['urlstring']);
            echo "
                var www_url='".SITE_URL."';
                document.write(\"<SCRIPT LANGUAGE='JavaScript' src='".SITE_URL."/data/js/player.js'><\/SCRIPT>\");
                playurl='$urlstring';
                ";
            exit;
    }
   //div在线编辑开启
   function divstart()
   {

		   if(isset($GLOBALS['SCup']->SCup_user_msg['data']['view_start_or_stop'])&&$GLOBALS['SCup']->SCup_user_msg['data']['view_start_or_stop']==1)
		   {
		   $GLOBALS['SCup']->SCup_user_msg['data']['view_start_or_stop']=0;		   
		   }else
		   {
		   $GLOBALS['SCup']->SCup_user_msg['data']['view_start_or_stop']=1;		   
		   }
		   $notice = &get_message(array('msg'=>"Succes.......",'url'=>FROM_URL,'title'=>'Notice'));
		   $notice ->display();				

		
   }


}
?>